package com.hxc.common.bean.dataDb;

import com.hxc.common.bean.ZdMarkDown;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ZdMarkDownDao {

  private static final String SQL_INS = "INSERT INTO zd_mark_down(handle,file_name,file_path,file_content,type,create_time,update_time,file_type,key_word) VALUES (?,?,?,?,?,?,?,?,?)";

  private static final String SQL_UPD = "UPDATE zd_mark_down SET file_name=?,file_path=?,file_content=?,type=?,create_time=?,update_time=?,file_type=?,key_word=? WHERE handle=?";

  private static final String SQL_SEL = "SELECT handle,file_name,file_path,file_content,type,create_time,update_time,file_type,key_word FROM zd_mark_down ";

  private final Connection conn;

  public ZdMarkDownDao(Connection conn) {
    this.conn = conn;
  }

  public int insert(ZdMarkDown data) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_INS)) {
      ps.setString(1, data.getHandle());
      ps.setString(2, data.getFileName());
      ps.setString(3, data.getFilePath());
      ps.setString(4, data.getFileContent());
      ps.setString(5, data.getType());
      ps.setTimestamp(6, new Timestamp(data.getCreateTime().getTime()));
      ps.setTimestamp(7, new Timestamp(data.getUpdateTime().getTime()));
      ps.setString(8, data.getFileType());
      ps.setString(9, data.getKeyWord());

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int insert(List<ZdMarkDown> dataList) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_INS)) {
      for (ZdMarkDown data : dataList) {
        ps.setString(1, data.getHandle());
        ps.setString(2, data.getFileName());
        ps.setString(3, data.getFilePath());
        ps.setString(4, data.getFileContent());
        ps.setString(5, data.getType());
        ps.setTimestamp(6, new Timestamp(data.getCreateTime().getTime()));
        ps.setTimestamp(7, new Timestamp(data.getUpdateTime().getTime()));
        ps.setString(8, data.getFileType());
        ps.setString(9, data.getKeyWord());

        ps.addBatch();
      }
      return ps.executeBatch().length;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int update(ZdMarkDown data) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_UPD)) {
      ps.setString(1, data.getFileName());
      ps.setString(2, data.getFilePath());
      ps.setString(3, data.getFileContent());
      ps.setString(4, data.getType());
      ps.setTimestamp(5, new Timestamp(data.getCreateTime().getTime()));
      ps.setTimestamp(6, new Timestamp(data.getUpdateTime().getTime()));
      ps.setString(7, data.getFileType());
      ps.setString(8, data.getKeyWord());
      ps.setString(9, data.getHandle());

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int update(List<ZdMarkDown> dataList) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_UPD)) {
      for (ZdMarkDown data : dataList) {
        ps.setString(1, data.getFileName());
        ps.setString(2, data.getFilePath());
        ps.setString(3, data.getFileContent());
        ps.setString(4, data.getType());
        ps.setTimestamp(5, new Timestamp(data.getCreateTime().getTime()));
        ps.setTimestamp(6, new Timestamp(data.getUpdateTime().getTime()));
        ps.setString(7, data.getFileType());
        ps.setString(8, data.getKeyWord());
        ps.setString(9, data.getHandle());

        ps.addBatch();
      }
      return ps.executeBatch().length;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int delete(String handle) {
    try (PreparedStatement ps = this.conn
        .prepareStatement("DELETE FROM zd_mark_down WHERE handle=?")) {
      ps.setString(1, handle);

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public List<ZdMarkDown> selectAll() {
    ArrayList<ZdMarkDown> result = new ArrayList<ZdMarkDown>();
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_SEL)) {

      ResultSet rs = ps.executeQuery();
      while (rs.next()) {
        result.add(convert(rs));
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public List<ZdMarkDown> selectByType(String type) {
    ArrayList<ZdMarkDown> result = new ArrayList<ZdMarkDown>();
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_SEL + "WHERE type=?")) {
      ps.setString(1, type);
      ResultSet rs = ps.executeQuery();
      while (rs.next()) {
        result.add(convert(rs));
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }


  public ZdMarkDown selectByPK(String handle) {
    ZdMarkDown result = null;
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_SEL + "WHERE handle=?")) {
      ps.setString(1, handle);

      ResultSet rs = ps.executeQuery();
      if (rs.next()) {
        result = convert(rs);
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public ZdMarkDown selectByFileNameAndFilePath(String fileName, String filePath) {
    ZdMarkDown result = null;
    try (PreparedStatement ps = this.conn
        .prepareStatement(SQL_SEL + "WHERE file_name=? and file_path=?")) {
      ps.setString(1, fileName);
      ps.setString(2, filePath);

      ResultSet rs = ps.executeQuery();
      if (rs.next()) {
        result = convert(rs);
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }


  private ZdMarkDown convert(ResultSet rs) throws SQLException {
    ZdMarkDown data = new ZdMarkDown();
    int index = 1;
    data.setHandle(rs.getString(index++));
    data.setFileName(rs.getString(index++));
    data.setFilePath(rs.getString(index++));
    data.setFileContent(rs.getString(index++));
    data.setType(rs.getString(index++));
    data.setCreateTime(rs.getTimestamp(index++));
    data.setUpdateTime(rs.getTimestamp(index++));
    data.setFileType(rs.getString(index++));
    data.setKeyWord(rs.getString(index++));

    return data;
  }
}
